/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.peng.service.impl;

import me.peng.domain.RecyclerFind;
import me.peng.domain.Recyclerhome;
import me.peng.rest.FileController;
import me.peng.utils.ValidationUtil;
import me.peng.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.peng.repository.RecyclerhomeRepository;
import me.peng.service.RecyclerhomeService;
import me.peng.service.dto.RecyclerhomeDto;
import me.peng.service.dto.RecyclerhomeQueryCriteria;
import me.peng.service.mapstruct.RecyclerhomeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.peng.utils.PageUtil;
import me.peng.utils.QueryHelp;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author pengjianye
* @date 2021-10-13
**/
@Service
@RequiredArgsConstructor
public class RecyclerhomeServiceImpl implements RecyclerhomeService {

    private final RecyclerhomeRepository recyclerhomeRepository;
    private final RecyclerhomeMapper recyclerhomeMapper;

    @Override
    public Map<String,Object> queryAll(RecyclerhomeQueryCriteria criteria, Pageable pageable){
        Page<Recyclerhome> page = recyclerhomeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(recyclerhomeMapper::toDto));
    }

    @Override
    public List<RecyclerhomeDto> queryAll(RecyclerhomeQueryCriteria criteria){
        return recyclerhomeMapper.toDto(recyclerhomeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public RecyclerhomeDto findById(Integer id) {
        Recyclerhome recyclerhome = recyclerhomeRepository.findById(id).orElseGet(Recyclerhome::new);
        ValidationUtil.isNull(recyclerhome.getId(),"Recyclerhome","id",id);
        return recyclerhomeMapper.toDto(recyclerhome);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RecyclerhomeDto create(Recyclerhome resources) {

        if (FileController.header.length()!=0){
            resources.setHeader(FileController.header);
            FileController.header="";
        }
        if (FileController.body.length()!=0){
            resources.setBody(FileController.body);
            FileController.body="";
        }
        if (FileController.tail.length()!=0){
            resources.setTail(FileController.tail);
            FileController.tail="";
        }
        return recyclerhomeMapper.toDto(recyclerhomeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Recyclerhome resources) {
        if (resources.getHeader()!=null){
            //删除Header图片资源
            String[] body =resources.getHeader().split(",");
            for (String s:body ){
                FileServiceImpl.deleteFileData(s);
            }
        }

        //获取修改的值
        if (FileController.header.length()!=0){
            resources.setHeader(FileController.header);
            FileController.header="";
        }
        //删除Body
        if (resources.getBody()!=null){
            String[] body =resources.getBody().split(",");
            for (String s:body ){
                FileServiceImpl.deleteFileData(s);
            }

        }
        if (FileController.body.length()!=0){
            resources.setBody(FileController.body);
            FileController.body="";
        }

        //删除Tail
        if (resources.getTail()!=null) {
            String[] details = resources.getTail().split(",");
            for (String s : details) {
                FileServiceImpl.deleteFileData(s);
            }
        }
        if (FileController.tail.length()!=0){
            resources.setTail(FileController.tail);
            FileController.tail="";
        }

        Recyclerhome recyclerhome = recyclerhomeRepository.findById(resources.getId()).orElseGet(Recyclerhome::new);
        ValidationUtil.isNull( recyclerhome.getId(),"Recyclerhome","id",resources.getId());
        recyclerhome.copy(resources);
        recyclerhomeRepository.save(recyclerhome);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {

            Optional<Recyclerhome> resul=recyclerhomeRepository.findById(id);

            if (resul.isPresent()&&resul.get().getHeader()!=null){
                //删除操作
                String[] strings=resul.get().getHeader().split(",");
                for (String s:strings){
                    FileServiceImpl.deleteFileData(s);
                }
            }
            if (resul.isPresent()&&resul.get().getBody()!=null){
                //删除操作
                String[] strings=resul.get().getBody().split(",");
                for (String s:strings){
                    FileServiceImpl.deleteFileData(s);
                }
            }
            if (resul.isPresent()&&resul.get().getTail()!=null){
                //删除操作
                String[] strings=resul.get().getTail().split(",");
                for (String s:strings){
                    FileServiceImpl.deleteFileData(s);
                }
            }

            recyclerhomeRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<RecyclerhomeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RecyclerhomeDto recyclerhome : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("头部页面", recyclerhome.getHeader());
            map.put("中部页面", recyclerhome.getBody());
            map.put("尾部页面", recyclerhome.getTail());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
